package com.lzs.baseruku.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzs.baseruku.service.SupplierService;
import com.lzs.entity.Supplier;
import com.lzs.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ymx
 * @since 2020-11-24
 */
@RestController
@RequestMapping("/baseruku/supplier")
@Api(value = "供应商管理文档")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    @ApiOperation(value = "查询所有的供应商")
    @PostMapping("/selectSupplier/{page}/{limit}")
    public R selectSupplier(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody Supplier supplier){
        Page<Supplier> supplierPage = supplierService.selectSupplier(page, limit, supplier);
        return R.ok().data("supplierPage",supplierPage);
    }

    @ApiOperation(value = "根据ID查询供应商")
    @GetMapping("/selectSupplierById/{id}")
    public R selectSupplierById(@PathVariable Long id){
        Supplier supplier = this.supplierService.getById(id);
        return R.ok().data("supplier",supplier);
    }

    @ApiOperation(value = "新增或修改供应商")
    @PostMapping("/insertOrUpdateSupplier")
    public R insertOrUpdateSupplier(@RequestBody Supplier supplier){

            boolean b = this.supplierService.saveOrUpdate(supplier);
            if (b){
                return R.ok().message("操作成功");
            }else {
                return R.error().message("操作失败");
            }
    }

    @ApiOperation(value = "根据ID删除供应商")
    @DeleteMapping("/deleteSupplier/{id}")
    public R deleteSupplier(@PathVariable Long id){
        boolean b = this.supplierService.removeById(id);
        if (b){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }

}

